import 'dart:io';

import 'package:valovault_reminder/util/val_screen_size_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

enum ValAppBarType {
  login,
  normal,
  secondLevel,
}

class ValAppCommonBar extends StatefulWidget implements PreferredSizeWidget {
  final double barHeight;
  final String title;
  final ValAppBarType style;
  final Color backgroundColor;
  final Color titleColor;
  final String? backgroundImageName;
  final Widget? leadingWidget;
  final IconData? icon;
  final Widget? trailingWidget;
  final Widget? centerWidget;
  final Brightness? brightness;
  final double elevation;
  const ValAppCommonBar({
    Key? key,
    this.barHeight = 88,
    this.title = '',
    this.style = ValAppBarType.normal,
    this.backgroundColor = Colors.white,
    this.titleColor = const Color(0xE6000000),
    this.backgroundImageName,
    this.leadingWidget,
    this.icon,
    this.trailingWidget,
    this.centerWidget,
    this.brightness,
    this.elevation = 0,
  }) : super(key: key);

  @override
  _ValAppCommonBarState createState() => _ValAppCommonBarState();
  @override // && Platform.isAndroid
  Size get preferredSize =>
      Size.fromHeight((style == ValAppBarType.secondLevel ? 20.0 : 0.0) +
          44.0 +
          (Platform.isAndroid
              ? ValScreenSizeKit.getInstance().appBarHeight
              : ValScreenSizeKit.getInstance().statusBarHeight));
  //108.0
  // : 44.0 +
  //     (Platform.isAndroid
  //         ? ScreenUtils.getInstance().appBarHeight
  //         : ScreenUtils.getInstance().statusBarHeight));

  // (Platform.isAndroid
  //     ? 44.0 +
  //         ScreenUtils.getInstance().appBarHeight +
  //         ScreenUtils.getInstance().statusBarHeight
  //     : 44.0 + ScreenUtils.getInstance().statusBarHeight));
}

class _ValAppCommonBarState extends State<ValAppCommonBar> {
  @override
  Widget build(BuildContext context) {
    final SystemUiOverlayStyle overlayStyle =
        widget.brightness == Brightness.dark
            ? SystemUiOverlayStyle.light
            : SystemUiOverlayStyle.dark;
    if (widget.style == ValAppBarType.login) {
      return loginAppbar(overlayStyle);
    } else if (widget.style == ValAppBarType.normal) {
      return normalAppbar(overlayStyle);
    } else {
      return secondLevelAppbar(overlayStyle);
    }
  }

  Widget loginAppbar(SystemUiOverlayStyle overlayStyle) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      //套AnnotatedRegion是为了增加状态栏控制
      value: overlayStyle,
      child: Material(
        //套Material是为了增加elevation
        elevation: widget.elevation,
        child: Container(
          height: 120,
          color: widget.backgroundColor,
          padding: EdgeInsets.only(left: 20, right: 20),
          child: SafeArea(
            child: Stack(alignment: Alignment.center, children: [
              ///左边
              Positioned(
                left: 0,
                top: 40,
                child: Container(
                  alignment: Alignment.topLeft,
                  child: widget.leadingWidget == null
                      ? GestureDetector(
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                          child: Icon(
                            widget.icon == null
                                ? Icons.arrow_back_ios
                                : widget.icon,
                            color: widget.titleColor ??
                                Color.fromRGBO(0, 0, 0, 0.9),
                          ))
                      : widget.leadingWidget,
                ),
              ),

              ///右边
              Positioned(
                right: 0,
                child: Container(
                  child: widget.trailingWidget,
                ),
              )
            ]),
          ),
        ),
      ),
    );
  }

  Widget normalAppbar(SystemUiOverlayStyle overlayStyle) {
    // print(
    //     "ScreenUtils.getInstance().statusBarHeight: ${CsScreenUtils.getInstance().statusBarHeight}");
    // print(
    //     "ScreenUtils.getInstance().appBarHeight: ${CsScreenUtils.getInstance().appBarHeight}");
    return AnnotatedRegion<SystemUiOverlayStyle>(
      //套AnnotatedRegion是为了增加状态栏控制
      value: overlayStyle,
      child: Material(
        //套Material是为了增加elevation
        elevation: widget.elevation,
        color: widget.backgroundColor,
        child: Container(
          // 100.0
          height: Platform.isAndroid
              ? 44.0 + ValScreenSizeKit.getInstance().appBarHeight
              : 44.0 + ValScreenSizeKit.getInstance().statusBarHeight,
          //
          // ?: widget.barHeight,
          color: widget.backgroundColor,
          padding: EdgeInsets.only(
              left: 20,
              right: 20, // ScreenUtils.getInstance().statusBarHeight
              top: Platform.isAndroid
                  ? ValScreenSizeKit.getInstance().appBarHeight
                  : 0),
          child: SafeArea(
            child: Stack(alignment: Alignment.center, children: [
              Align(
                alignment: Alignment.center,
                child: Container(
                  margin: EdgeInsets.only(left: 20, right: 20),
                  child: widget.centerWidget == null ? Text(
                    widget.title,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        color:
                            widget.titleColor ?? Color.fromRGBO(0, 0, 0, 0.9),
                        fontSize: ValScreenSizeKit.getInstance().getWidth(18),
                        fontWeight: FontWeight.w500),
                    overflow: TextOverflow.ellipsis,
                  ) : widget.centerWidget,
                ),
              ),
              //左右
              Positioned(
                left: 0,
                right: 0,
                top: 0,
                bottom: 0,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    GestureDetector(
                      child: widget.leadingWidget == null
                          ? GestureDetector(
                              onTap: () {
                                Navigator.of(context).pop();
                              },
                              child: Icon(
                                widget.icon == null
                                    ? Icons.arrow_back_ios
                                    : widget.icon,
                                color: widget.titleColor ??
                                    Color.fromRGBO(0, 0, 0, 0.9),
                              ))
                          : widget.leadingWidget,
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                    ),
                    Container(
                      child: widget.trailingWidget,
                    ),
                  ],
                ),
              ),
            ]),
          ),
        ),
      ),
    );
  }

  Widget secondLevelAppbar(SystemUiOverlayStyle overlayStyle) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      //套AnnotatedRegion是为了增加状态栏控制
      value: overlayStyle,
      child: Material(
        //套Material是为了增加elevation
        elevation: widget.elevation,

        child: Container(
          height: 20.0 +
              44.0 +
              (Platform.isAndroid
                  ? ValScreenSizeKit.getInstance().appBarHeight
                  : ValScreenSizeKit.getInstance().statusBarHeight),
          // Platform.isAndroid
          // ?: 70 + ScreenUtils.getStatusBarH(context),
          color: widget.backgroundColor,
          padding: EdgeInsets.only(
            left: 20,
            right: 20,
            top:
                Platform.isAndroid ? ValScreenSizeKit.getInstance().appBarHeight : 0,
          ),
          child: SafeArea(
            child: Stack(alignment: Alignment.center, children: [
              Positioned(
                  left: 0,
                  right: 0,
                  top: 0,
                  // top: Platform.isAndroid ? 40.0 : 5.0,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          GestureDetector(
                            child: Icon(
                              Icons.arrow_back_ios,
                              color: Color.fromRGBO(0, 0, 0, 0.9),
                            ),
                            onTap: () {
                              Navigator.of(context).pop();
                            },
                          ),
                          widget.trailingWidget == null
                              ? Container()
                              : widget.trailingWidget!
                        ],
                      ),
                      SizedBox(
                        height: 8,
                      ),
                      Container(
                        child: Text(
                          widget.title,
                          style: TextStyle(
                              color: widget.titleColor ??
                                  Color.fromRGBO(0, 0, 0, 0.9),
                              fontSize: 24,
                              fontWeight: FontWeight.w500),
                        ),
                      )
                    ],
                  )),
            ]),
          ),
        ),
      ),
    );
  }
}
